package com.baic.ibase.controller.api.v1;


import com.baic.ibase.models.dao.Users;
import com.baic.ibase.models.req.common.ApiResponse;
import com.baic.ibase.models.req.common.IdRequest;
import com.baic.ibase.models.req.common.PageResponse;
import com.baic.ibase.models.req.users.CreateUserReq;
import com.baic.ibase.models.req.users.DeleteUserReq;
import com.baic.ibase.models.req.users.ListUsersByPageReq;
import com.baic.ibase.models.vo.UserInfo;
import com.baic.ibase.service.users.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


@Slf4j
@RestController
@RequestMapping("/api/v1/user")
public class UserController {

    @Autowired
    private UsersService usersService;


    /**
     * @api {POST} /api/v1/user/ListUsersByPage 查询用户列表
     * @apiVersion 1.0.0
     * @apiGroup UserController
     * @apiName listUsersByPage()
     * @apiParam (请求体) {Number} userId
     * @apiParam (请求体) {String} username
     * @apiParam (请求体) {String} nickname
     * @apiParam (请求体) {String} phone
     * @apiParam (请求体) {Number} status
     * @apiParam (请求体) {Number} pageNum
     * @apiParam (请求体) {Number} pageSize
     * @apiParam (请求体) {String} sort
     * @apiParam (请求体) {String} order
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"phone":"j","requestId":"M2djY7A","nickname":"YMylNTSnXT","pageSize":8470,"sort":"MGentIB0pW","userId":4846,"pageNum":8006,"username":"vMyZcl","status":5315,"order":"U8txFCvuat"}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {String} data.requestId
     * @apiSuccess (响应结果) {Number} data.pageNum
     * @apiSuccess (响应结果) {Number} data.pageSize
     * @apiSuccess (响应结果) {Number} data.total
     * @apiSuccess (响应结果) {Number} data.pages
     * @apiSuccess (响应结果) {Array} data.data
     * @apiSuccess (响应结果) {Number} data.data.id
     * @apiSuccess (响应结果) {String} data.data.username 用户名
     * @apiSuccess (响应结果) {String} data.data.password 密码
     * @apiSuccess (响应结果) {String} data.data.nickname 昵称
     * @apiSuccess (响应结果) {String} data.data.avatar 头像
     * @apiSuccess (响应结果) {Number} data.data.status 状态
     * @apiSuccess (响应结果) {String} data.data.phone 手机
     * @apiSuccess (响应结果) {Object} data.data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"rb6QYOE7bZ","code":8444,"data":{"total":6335,"pages":8232,"data":[{"password":"SsdTxL","createdUserId":3382,"phone":"8QH15dY7lO","nickname":"Lpe9","id":2861,"avatar":"gSoLY","updatedUserId":4280,"username":"2Y02YSyF","status":2510}],"requestId":"zCywE","pageSize":6143,"pageNum":802},"success":false,"message":"nVHRbGt5ik","errors":"a8sKU"}
     */
    @PostMapping("/ListUsersByPage")
    public ApiResponse<PageResponse<Users>> listUsersByPage(@RequestBody ListUsersByPageReq req) {
        return usersService.listUsersByPage(req);
    }


    /**
     * @api {POST} /api/v1/user/CreateUser 新增用户
     * @apiVersion 1.0.0
     * @apiGroup UserController
     * @apiName addUsers()
     * @apiParam (请求体) {String} username
     * @apiParam (请求体) {String} nickname
     * @apiParam (请求体) {String} phone
     * @apiParam (请求体) {String} password
     * @apiParamExample 请求体示例
     * {"password":"yFQa","phone":"Ij","nickname":"Kr2Mnq","username":"1aHTp"}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.username 用户名
     * @apiSuccess (响应结果) {String} data.password 密码
     * @apiSuccess (响应结果) {String} data.nickname 昵称
     * @apiSuccess (响应结果) {String} data.avatar 头像
     * @apiSuccess (响应结果) {Number} data.status 状态
     * @apiSuccess (响应结果) {String} data.phone 手机
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"pjVo449jG","code":8003,"data":{"password":"y7mcKW","createdUserId":5452,"phone":"2ib8gSsos","nickname":"7hwzoHLl","id":6397,"avatar":"aqRb0VO0","updatedUserId":495,"username":"l84K73P","status":6868},"success":true,"message":"L","errors":"wNGjv"}
     */
    @PostMapping("/CreateUser")
    public ApiResponse<Users> addUsers(@RequestBody CreateUserReq req) {
        return usersService.saveUsers(req);
    }

    /**
     * @api {POST} /api/v1/user/DeleteUser 删除用户
     * @apiVersion 1.0.0
     * @apiGroup UserController
     * @apiName deleteUsers()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"requestId":"B5gQFp1K9U","id":6979}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.username 用户名
     * @apiSuccess (响应结果) {String} data.password 密码
     * @apiSuccess (响应结果) {String} data.nickname 昵称
     * @apiSuccess (响应结果) {String} data.avatar 头像
     * @apiSuccess (响应结果) {Number} data.status 状态
     * @apiSuccess (响应结果) {String} data.phone 手机
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"Qw6Enha7S","code":2805,"data":{"password":"3jL","createdUserId":4618,"phone":"2oJlLoNcG","nickname":"9rW","id":3477,"avatar":"bOpV","updatedUserId":7303,"username":"2oU","status":9716},"success":true,"message":"NMqdMR","errors":"745a02Ry46"}
     */
    @PostMapping("/DeleteUser")
    public ApiResponse<Users> deleteUsers(@RequestBody DeleteUserReq req) {
        boolean b = usersService.removeById(req.getId());
        log.info("deleteUsers req:{}, resp: {}", req, b);
        return b ? ApiResponse.ok() : ApiResponse.error(500, "删除失败");
    }

    /**
     * @api {POST} /api/v1/user/UpdateUser 更新用户信息
     * @apiVersion 1.0.0
     * @apiGroup UserController
     * @apiName updateUsers()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} username 用户名
     * @apiParam (请求体) {String} password 密码
     * @apiParam (请求体) {String} nickname 昵称
     * @apiParam (请求体) {String} avatar 头像
     * @apiParam (请求体) {Number} status 状态
     * @apiParam (请求体) {String} phone 手机
     * @apiParam (请求体) {Object} createdTime 创建时间
     * @apiParam (请求体) {Object} updatedTime 更新时间
     * @apiParam (请求体) {Number} createdUserId 创建人
     * @apiParam (请求体) {Number} updatedUserId 更新人
     * @apiParamExample 请求体示例
     * {"password":"7T4eH5","createdUserId":2234,"phone":"F74n5gf","nickname":"Exda","id":6018,"avatar":"8shIhyprwG","updatedUserId":3482,"username":"2Pv","status":6916}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.username 用户名
     * @apiSuccess (响应结果) {String} data.password 密码
     * @apiSuccess (响应结果) {String} data.nickname 昵称
     * @apiSuccess (响应结果) {String} data.avatar 头像
     * @apiSuccess (响应结果) {Number} data.status 状态
     * @apiSuccess (响应结果) {String} data.phone 手机
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"4E","code":7548,"data":{"password":"g","createdUserId":5795,"phone":"rSSkzCC","nickname":"hNn","id":9633,"avatar":"JDtha","updatedUserId":9732,"username":"hlh","status":9603},"success":true,"message":"VIqFrtx","errors":"bIMKkcYhbh"}
     */
    @PostMapping("/UpdateUser")
    public ApiResponse<Users> updateUsers(@RequestBody Users req) {
        req.setUpdatedTime(LocalDateTime.now());

        if (usersService.updateById(req)) {
            log.info("updateUsers req:{}, resp: {}", req, true);
            return ApiResponse.ok(req);
        } else {
            log.info("updateUsers req:{}, resp: {}", req, false);
            return ApiResponse.error(500, "更新失败");
        }
    }

    /**
     * @api {POST} /api/v1/user/GetUser getUser()
     * @apiVersion 1.0.0
     * @apiGroup UserController
     * @apiName getUser()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"requestId":"IFEvQDuFE","id":4615}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.username 用户名
     * @apiSuccess (响应结果) {String} data.password 密码
     * @apiSuccess (响应结果) {String} data.nickname 昵称
     * @apiSuccess (响应结果) {String} data.avatar 头像
     * @apiSuccess (响应结果) {Number} data.status 状态
     * @apiSuccess (响应结果) {String} data.phone 手机
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"riy","code":774,"data":{"password":"px","createdUserId":6597,"phone":"T492MI","nickname":"tAMlR0","id":2413,"avatar":"dU","updatedUserId":9295,"username":"c96Txy1chs","status":4526},"success":true,"message":"gSB","errors":"ZZdhmg"}
     */
    @PostMapping("/GetUser")
    public ApiResponse<Users> getUser(@RequestBody IdRequest req) {
        Users user = usersService.getById(req.getId());
        log.info("getUser req:{}, resp: {}", req, user);
        return ApiResponse.ok(user);
    }

    /**
     * @api {POST} /api/v1/user/Info 查询用户详情、角色、权限
     * @apiVersion 1.0.0
     * @apiGroup UserController
     * @apiName info()
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=5NasP
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.userName
     * @apiSuccess (响应结果) {Number} data.status
     * @apiSuccess (响应结果) {String} data.avatar
     * @apiSuccess (响应结果) {String} data.phone
     * @apiSuccess (响应结果) {Number} data.roleId
     * @apiSuccess (响应结果) {Object} data.roles
     * @apiSuccess (响应结果) {Number} data.roles.id
     * @apiSuccess (响应结果) {String} data.roles.roleCode
     * @apiSuccess (响应结果) {String} data.roles.roleName
     * @apiSuccess (响应结果) {Array} data.roles.permissions
     * @apiSuccessExample 响应结果示例
     * {"traceId":"BdpyfOrp","code":6149,"data":{"phone":"34kG8Az1Zq","roleId":3454,"roles":{"permissions":[],"roleCode":"jyrE8PBKs","roleName":"cO54BV9xfF","id":3202},"id":2933,"avatar":"HR","userName":"KgHELdGs","status":7411},"success":false,"message":"VbPpbtls","errors":"DnQo6H"}
     */
    @PostMapping("/Info")
    public ApiResponse<UserInfo> info(@RequestHeader String token) {
        return usersService.getInfoByToken(token);
    }

}
